#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>

int i,j,index_min_particle,iteration;
float c1=2.0,c2=2.0;


// declaring all needed variables for simulation
void declare_problem_variables()
{
int k=100;		   
int ability_level[100];   
int expected_learning_tagets [100][10];  
int l_time_course[100];   
int u_time_course[100];  

int m=10;		
int n=200;              
int difficulty_level[200]; 
int r[200][10];            
int time_material[200];    
}

void initialize_problem_variables()
{
}

// refers to the position of the particles
struct position
{
 double position_value;
 long int position_vector[200];
};

// refers to the particles of the swarm 
struct particle
{
 struct position current_pos;
 double velocity[200];  
 struct position pbest;
};


// refers to the swarm itself
struct swarm
{
 struct position gbest;
 struct particle p[20];
}s;

void fitness_value()
{
}

// calculating the velocity of the particles of the swarm
void calculate_velocity(int i,int j)
{
  float component1,component2;
  component1=(c1*/* random number1 */* (s.p[i].pbest.position_vector[j] - s.p[i].current_pos.position_vector[j]);
  component2=(c2*/* random number2 */* (s.gbest.position_vector[j] - s.p[i].current_pos.position_vector[j]);
  s.p[i].velocity[j] += (component1+component2);
}
// scaling for velocity adjustment
void sigmoid_scaling(int i,j)
{
   s.p[i].velocity[j] = 1 / (1+/*exp^-s.p[i].velocity[j]*/ );
}
// personal best calculation
void pbest()
{
  for(i=0;i<20;i++)
  {
     if(s.p[i].current_pos.position_value< s.p[i].pbest.position_value)
     {
      s.p[i].pbest.position_value = s.p[i].current_position.position_value;

      for(j=0;j<200;j++)
      {
       s.p[i].pbest.position_vector = s.p[i].current_position.position_vector;
      }
     }
  }
}
// global best calculation
void gbest()
{
 index_min_particle=0; 

 for(i=1;i<20;i++)
 {
  if(s.p[i].pbest.position_value < s.p[index_min_particle].pbest.position_value)
  index_min_particle=i;
 }

 s.gbest.position_value = s.p[index_min_particle].pbest.position_value;
 for(j=0;j<200;j++)
 {
  s.gbest.position_vector[j] = s.p[index_min_particle].pbest.position_vector[j];
 }
 ++iteration;
}

// main function 
void main()
{
  declare_problem_variables();
  initialize_problem_variables();
  
  for(i=0;i<20;i++)
  {
   for(j=0;j<200;j++)
   {
    s.p[i].current_pos.position_vector[j];  
    s.p[i].velocity[j];                     
   }
  }
  
  for(i=0;i<20;i++)
  {
   fitness_value(i);
  }
  
  for(i=0;i<20;i++)
  {
   s.p[i].pbest.position_value = s.p[i].current_pos.position_value;

   for(j=0;j<200;j++)
   {
    s.p[i].pbest.position_vector[j] = s.p[i].current_position.position_vector[j];
   }
  }
 
 gbest();   
 iteration=1;  
  
 while(iteration <= 200)  
 {
   
   for(i=0;i<20;i++)
   {
     for(j=0;j<200;j++)
     {
      calculate_velocity(i,j);  
      sigmoid_scaling(i,j);     
     }
    }
 
   for(i=0;i<20;i++)
   {
     for(j=0;j<200;j++)
     {
       if(/*rand_number*/ < s.p[i].velocity[j]) 
	        s.p[i].current_pos.position_vector[j]=1;
       else
	        s.p[i].current_pos.position_vector[j]=0;
     }
    }
    
    for(i=0;i<20;i++)
    {
      fitness_value(i);
    }
    
    pbest();
    gbest();
 }
}